// 对条件变量进行封装

#ifndef _COND_HPP__
#define _COND_HPP__

#include <iostream>
#include <pthread.h>

//条件锁
class Cond
{
public:
    Cond()
    {
        pthread_cond_init(&cd, nullptr);
    }

    void Wait(pthread_mutex_t* mutex)
    {
        int n = pthread_cond_wait(&cd, mutex);
        if (n != 0)
            printf("等待线程失败\n");
    }
    
    void Notify()
    {
        int n = pthread_cond_signal(&cd);
        if (n != 0)
            printf("唤醒单个线程失败\n");
    }

    void NotifyAll()
    {
        int n = pthread_cond_broadcast(&cd);
        if (n != 0)
            printf("唤醒所有线程失败\n");
    }

    ~Cond()
    {
        pthread_cond_destroy(&cd);
    }

private:
    pthread_cond_t cd;
};
#endif